package com.nlx.notes.module.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nlx.notes.module.bean.vo.category.IconMaterialVO;
import com.nlx.notes.module.entity.Categories;
import com.nlx.notes.module.entity.IconMaterial;
import com.nlx.notes.module.entity.Transactions;
import com.nlx.notes.module.entity.Users;
import com.nlx.notes.module.service.ICategoriesService;
import com.nlx.notes.module.service.IIconMaterialService;
import com.nlx.notes.module.service.ITransactionsService;
import com.nlx.notes.module.service.statistics.CategoryMonthStatisticsManageService;
import com.nlx.notes.module.service.user.IUsersService;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author nlx
 * @since 2025-08-30
 */
@Slf4j
@RestController
@RequestMapping("/iconMaterial")
@RequiredArgsConstructor
public class IconMaterialController {

    final IIconMaterialService iconMaterialService;

    final ICategoriesService categoriesService;

    final IUsersService usersService;

    final ITransactionsService transactionsService;



    @GetMapping(value = "/v1/list")
    public List<IconMaterialVO> iconList(@RequestParam(value = "iconType") String iconType){
        log.info("查询icon素材列表");
        return iconMaterialService.iconList(iconType);
    }



//    @PostConstruct
    @Transactional(rollbackFor = Exception.class)
    public void init() throws IOException {
//
//        List<Categories> categories = categoriesService.findListByUser(null, 0L);
//
//        List<Users> usersList = usersService.list();
//
//
//        for (Users user : usersList) {
//            List<Transactions> transactionsList = transactionsService.list(Wrappers.lambdaQuery(Transactions.class)
//                    .eq(Transactions::getUserId,user.getId())
//                    .eq(Transactions::getDelFlag, 0));
//
//            if(CollUtil.isEmpty(transactionsList)){
//                continue;
//            }
//            log.info("便利user：{}",user.getId());
//            //旧的分类id - 分类code
//            categories = categories.stream().filter(x -> StrUtil.isNotEmpty(x.getCategoryCode())).toList();
//
//
//
//            Set<Long> categoryIds = transactionsList.stream().map(Transactions::getCategoryId).collect(Collectors.toSet());
//
//
//            Set<Long> secondCategoryIds = transactionsList.stream().map(Transactions::getSecondCategoryId).collect(Collectors.toSet());
//
//
//            List<Categories> userCategories = categoriesService.findListByUser(null, user.getId());
//
//
//
//            Map<String, Long> userCategoryMap = userCategories.stream()
//                    .collect(Collectors.toMap(
//                            Categories::getCategoryCode,
//                            Categories::getId,
//                            (existing, replacement) -> replacement
//                    ));
//            List<Categories> categoriesList = categoriesService.list(Wrappers.lambdaQuery(Categories.class).in(Categories::getId, categoryIds));
//            for (Categories category : categoriesList) {
//                Long categoryId = userCategoryMap.get(category.getCategoryCode());
//                log.info("老id：{}，新id：{}", category.getId(), categoryId);
//                transactionsService.update(Wrappers.lambdaUpdate(Transactions.class)
//                        .set(Transactions::getCategoryId,categoryId)
//                        .eq(Transactions::getCategoryId,category.getId())
//                        .eq(Transactions::getUserId,user.getId()));
//            }
//
//            List<Categories> secondCategoriesList = categoriesService.list(Wrappers.lambdaQuery(Categories.class).in(Categories::getId, secondCategoryIds));
//            for (Categories category : secondCategoriesList) {
//                Long categoryId = userCategoryMap.get(category.getCategoryCode());
//                log.info("二级 老id：{}，新id：{}", category.getId(), categoryId);
//                transactionsService.update(Wrappers.lambdaUpdate(Transactions.class)
//                        .set(Transactions::getSecondCategoryId,categoryId)
//                                .eq(Transactions::getSecondCategoryId,category.getId())
//                        .eq(Transactions::getUserId,user.getId()));
//            }
//
//        }



//        Resource resource = new ClassPathResource("/static/img/categoryicon/defaulticon");
//        File directory = resource.getFile();
//        // 获取目录下所有文件名
//        String[] fileNames = directory.list();
//        if (fileNames != null) {
//            for (String fileName : fileNames) {
//                IconMaterial iconMaterial = new IconMaterial();
//                iconMaterial.setIconType("category");
//                iconMaterial.setIconName(fileName);
//                iconMaterial.setIconUrl("/img/categoryicon/defaulticon/"+fileName);
//                iconMaterial.setRemarks("/img/categoryicon/"+fileName);
//                iconMaterialService.save(iconMaterial);
//            }
//        }
//        System.out.println("保存完毕");
//        List<Categories> categories = categoriesService.findListByUser(null, 0L);
//        categories = categories.stream().filter(x -> StrUtil.isNotEmpty(x.getCategoryIcon())).toList();
//        List<String> iconUrls = categories.stream().map(Categories::getCategoryIcon).toList();
//        List<IconMaterial> materials = iconMaterialService.list(Wrappers.lambdaQuery(IconMaterial.class).in(IconMaterial::getRemarks, iconUrls));
//        Map<String,Long> iconMap = materials.stream().collect(Collectors.toMap(IconMaterial::getRemarks, IconMaterial::getId));
//        for (Categories c : categories) {
//            categoriesService.update(Wrappers.lambdaUpdate(Categories.class)
//                    .set(Categories::getCategoryIconId,iconMap.get(c.getCategoryIcon()))
//                    .eq(Categories::getId,c.getId()));
//        }
    }
}
